package org.jwcloud.project.module.zx.common.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.jwcloud.project.module.zx.baseManager.service.repository.MatBaseManagerRepository;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;

@Service
@Transactional
public class RptUtServiceImpl extends BaseController implements RptUtService{

	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Override
	public Response<PageListSlice> findPagi(Pagination pagination, Map map, List list, String total) {
		// TODO Auto-generated method stub
		//linshi
		//String pagisize = (String) map.get("pagisize");
		//System.out.println("___" + map);
		return findPagi(buildBounds(pagination, map),list, total);
	}
	
	private Pagination buildBounds(Pagination pagination, Map params) {
		;
		//System.out.println("2222222222:" + pagination.getPageSize());
		//System.out.println();
		ListBounds listBounds = getBounds(pagination);
		//ListBounds listBounds = new ListBounds(pagination.getStartIndex(), pagination.getPageSize());
		//System.out.println("asd: " + listBounds.getOffset());
		params.put("offset", listBounds.getOffset());
		params.put("limit", listBounds.getLimit());
		return pagination;
	}
	
	private Response<PageListSlice> findPagi(Pagination pagination, List list, String total) {
        ListSlice slice = new ListSlice<>(Long.valueOf(total), list);
        return updateResp(pagination, slice);
	}

	@Override
	public Object commonQeryCodeList(String tableName) {
		// TODO Auto-generated method stub
		return rptCommonRepository.queryCodeList_qlc(new HashMap() {{this.put("tableName", tableName);}});
	}

	@Override
	public Object commonQeryCodeList_all(String tableName) {
		// TODO Auto-generated method stub
		return rptCommonRepository.queryCodeList_qlc(new HashMap() {{
			this.put("tableName", tableName);
			this.put("isAll", "yes");
		}});
	}

	private Object pageHelper(Map map) {
		// TODO Auto-generated method stub
		//currentPage从1开始，0默认为1
		Integer currentPage = 1;
		Integer pageSize = 10;
		if(map.get("currentPage") != null) {
			currentPage = Integer.valueOf((String)map.get("currentPage"));
		}else {
			map.put("currentPage", "1");
		}
		if(map.get("pageSize") != null) {
			pageSize = Integer.valueOf((String)map.get("pageSize"));
		}else {
			map.put("pageSize", "10");
		}
		//PageHelper.startPage(currentPage + 1, pageSize);
		//前端第一页传1
		PageHelper.startPage(currentPage + 0, pageSize);
		return null;
	}

	@Override
	public Map pageBuild_old(List list) {
		// TODO Auto-generated method stub
//		Response<PageListSlice>
		com.github.pagehelper.Page srcList = (Page) list;
		Map inner = new HashMap();
		inner.put("total", srcList.getTotal());
		inner.put("list", list);
		Map outer = new HashMap();
		outer.put("code", "0");
		outer.put("description", "");
		outer.put("payload", inner);
		
		return outer;
	}
	@Override
	public Map pageBuild(Supplier<List> supplier, Map param) {
		// TODO Auto-generated method stub
		pageHelper(param);
		List list = supplier.get();
		
		com.github.pagehelper.Page srcList = (Page) list;
		Map inner = new HashMap();
		inner.put("total", srcList.getTotal());
		inner.put("currentPage", Integer.valueOf((String)param.get("currentPage")));
		inner.put("list", list);
		
		return inner;
	}
}
